<!DOCTYPE html "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
    >
<meta charset="UTF-8">
<head th:include="include :: header"></head>

<body class="gray-bg">
<input type="hidden" id="memberId" name="memberId" th:value="${memberId}">
<div class="wrapper wrapper-content ">
    <div class="col-sm-12">
        <div class="ibox">
            <table id="exampleTable" data-mobile-responsive="true">
            </table>
        </div>
    </div>
</div>
</div>
<!--shiro控制bootstraptable行内按钮看见性 来自bootdo的创新方案 -->
<div>
    <script type="text/javascript">
        var s_edit_h = 'hidden';
    </script>
</div>
<div shiro:hasPermission="mp:product:update">
    <script type="text/javascript">
        s_edit_h = '';
    </script>
</div>
<div>
    <script type="text/javascript">
        var s_edit_d = 'hidden';
    </script>
</div>
<div shiro:hasPermission="mp:product:del">
    <script type="text/javascript">
        s_edit_d = '';
    </script>
</div>

</div>
<div th:include="include :: footer"></div>
<script type="text/javascript">

    var prefix = "/mp/member";
    $(function () {
        var memberId = $('#memberId').val();
        load(memberId);
        //query();
    });

    function load(memberId) {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get', // 服务器数据的请求方式 get or post
                    url: prefix + "/userList?memberId="+memberId, // 服务器数据的加载地址
                    //showRefresh: true,
                    //	showToggle : true,
                    //	showColumns : true,
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: true, // 设置为true会有隔行变色效果
                    dataType: "json", // 服务器返回的数据类型
                    pagination: true, // 设置为true会在底部显示分页条
                    // queryParamsType : "limit",
                    // //设置为limit则会发送符合RESTFull格式的参数
                    singleSelect: false, // 设置为true将禁止多选
                    // contentType : "application/x-www-form-urlencoded",
                    // //发送到服务器的数据编码类型
                    pageSize: 10, // 如果设置了分页，每页数据条数
                    pageNumber: 1, // 如果设置了分布，首页页码
                    //search : true, // 是否显示搜索框
                    showColumns: false, // 是否显示内容下拉框（选择显示的列）
                    sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
                    queryParams: function (params) {
                        return {
                            //说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
                            limit: params.limit,
                            offset: params.offset
                        };
                    },
                    // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
                    // queryParamsType = 'limit' ,返回参数必须包含
                    // limit, offset, search, sort, order 否则, 需要包含:
                    // pageSize, pageNumber, searchText, sortName,
                    // sortOrder.
                    // 返回false将会终止请求
                    columns: [
                        {
                            field: 'memberId',
                            title: '会员编号'
                        },
                        {
                            field: 'name',
                            title: '用户名称'
                        },
                        {
                            field: 'phone',
                            title: '手机号'
                        },
                        {
                            field: 'idCardNo',
                            title: '身份证号'
                        },
                        {
                            field: 'regTime',
                            title: '注册日期'
                        },
                        {
                            field: 'productName',
                            title: '申请产品名称'
                        },
                        {
                            field: 'isGainCommission',
                            title: '是否已发放佣金',
                            formatter: function (value, row, index) {
                                if (value == '1') {
                                    return '<span class="label label-danger">已发放</span>';
                                } else if (value == '0') {
                                    return '<span class="label label-primary">未发放</span>';
                                }else if (value == '2') {
                                    return '<span class="label label-primary">已拒绝</span>';
                                }else if (value == '3') {
                                    return '<span class="label label-primary">已撤销</span>';
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'userId',
                            align: 'center',
                            formatter: function (value, row, index) {
                                if(row.isGainCommission == '0'){
                                    var dtl1 = '<a class="btn btn-success btn-sm " href="#" mce_href="#" title="发放佣金" ' +
                                        'onclick="gain(\'' + value + '\')"><i class="fa fa-address-card-o"></i>发放佣金</a> ';
                                    var dtl2 = '<a class="btn btn-success btn-sm " href="#" mce_href="#" title="拒绝" ' +
                                        'onclick="reject(\'' + value + '\')"><i class="fa fa-address-card-o"></i>拒绝</a> ';

                                    return dtl1 + dtl2;
                                }else if(row.isGainCommission == '1'){
                                    var dtl = '<a class="btn btn-success btn-sm " href="#" mce_href="#" title="撤销佣金" ' +
                                        'onclick="cancel(\'' + value + '\')"><i class="fa fa-address-card-o"></i>撤销佣金</a> ';
                                    return dtl;
                                }else{
                                    return "";
                                }
                            }
                        }]
                });
    }


    function reLoad() {
        var opt = {
            query: {
                limit: 10,
                offset: 0,
                memberId: $('#memberId').val()
            }
        };
        $('#exampleTable').bootstrapTable('refresh', opt);
    }

    function gain(userId) {
        layer.confirm("确认要发放佣金吗?", {
            btn: ['确定', '取消']
            // 按钮
        }, function () {
            $.ajax({
                type: 'POST',
                data: {
                    "userId"  : userId,
                    "isGainCommission" : 1,
                    "memberId": $('#memberId').val()
                },
                url: prefix + '/gain',
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        $('#exampleTable').bootstrapTable('refresh');
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        }, function () {
        });
    }

    function reject(userId) {
        layer.confirm("确认拒绝吗?", {
            btn: ['确定', '取消']
            // 按钮
        }, function () {
            $.ajax({
                type: 'POST',
                data: {
                    "userId"  : userId,
                    "isGainCommission" : 2,
                    "memberId": $('#memberId').val()
                },
                url: prefix + '/gain',
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        $('#exampleTable').bootstrapTable('refresh');
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        }, function () {
        });
    }

    function cancel(userId) {
        layer.confirm("确认要撤销已发放佣金吗?", {
            btn: ['确定', '取消']
            // 按钮
        }, function () {
            $.ajax({
                type: 'POST',
                data: {
                    "userId"  : userId,
                    "memberId": $('#memberId').val()
                },
                url: prefix + '/cancel',
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        $('#exampleTable').bootstrapTable('refresh');
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        }, function () {
        });
    }



    function batchRemove(id) {
        var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组

        layer.confirm("确认要删除数据吗?", {
            btn: ['确定', '取消']
            // 按钮
        }, function () {
            $.ajax({
                type: 'POST',
                data: {
                    "id": id
                },
                url: prefix + '/remove',
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        reLoad();
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        }, function () {
        });

    }
</script>
</body>

</body>
</html>